package com.douyin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douyin.dto.Result;
import com.douyin.service.RecommendationService;
import com.douyin.vo.VideoVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/recommendations")
public class RecommendationController {

    @Resource
    private RecommendationService recommendationService;

    /**
     * 获取个性化推荐视频
     * @param userId 用户ID（登录用户必填）
     * @param pageNum 页码，默认1
     * @param pageSize 每页数量，默认10
     */
    @GetMapping("/personal")
    public Result personalRecommend(
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        if (userId == null) {
            // 未登录用户返回热门推荐
            return Result.ok(recommendationService.getHotRecommend(pageNum, pageSize));
        }

        return Result.ok(recommendationService.getPersonalRecommend(userId, pageNum, pageSize));
    }

    /**
     * 手动刷新推荐（调试用）
     * @param userId 用户ID
     */
    @PostMapping("/refresh")
    public Result refreshRecommend(@RequestParam Long userId) {
        recommendationService.refreshRecommendationCache(userId);
        return Result.ok();
    }
}